define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dormitory/distribute/index',
                    add_url: 'dormitory/distribute/add',
                    edit_url: 'dormitory/distribute/edit',
                    del_url: 'dormitory/distribute/del',
                    multi_url: 'dormitory/distribute/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'boy_count', title: __('Boy Count'), operate: false},
                        {field: 'girl_count', title: __('Girl Count'), operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                            {
                                name: 'boy',
                                title: '为男生分配床位',
                                text: '为男生分配床位',
                                icon: 'fa fa-heart',
                                classname: 'btn btn-success btn-xs btn-click',
                                click: function (e, data) {
                                    if (data.boy_count) {
                                        Backend.api.open('dormitory/distribute/select?gender=1&id=' + data.id, __('设置寝室分配方式'));
                                    } else {
                                        Backend.api.toastr.error('该学院男生人数为0,不能分配床位！');
                                    }
                                }
                            },
                            {
                                name: 'girl',
                                title: '为女生分配床位',
                                text: '为女生分配床位',
                                icon: 'fa fa-heart-o',
                                classname: 'btn btn-success btn-xs btn-click',
                                click: function (e, data) {
                                    if (data.girl_count) {
                                        Backend.api.open('dormitory/distribute/select?gender=2&id=' + data.id, __('设置寝室分配方式'));
                                    } else {
                                        Backend.api.toastr.error('该学院女生人数为0,不能分配床位！');
                                    }
                                }
                            }
                        ], formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 12

            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //按寝室分
            $(document).on("click", ".btn-dorm", function () {
                layer.closeAll();
                var id = $(this).data('id');
                var gender = $(this).data('gender');
                Backend.api.open('dormitory/distribute/dorm?gender=' + gender +'&id=' + id, __('按寝室分配'), {area: ['70%', '80%']});

            });

            //按床位分
            $(document).on("click", ".btn-bed", function () {
                layer.closeAll();

                var id = $(this).data('id');
                var gender = $(this).data('gender');
                Backend.api.open('dormitory/distribute/bed?gender=' + gender +'&id=' + id, __('按床位分配'), {area: ['70%', '80%']});
            });

            $(document).on('click', '.btn-back', function () {
                layer.closeAll();
                var id = $('#faculty_id').val();
                var gender = $('#gender').val();
                Backend.api.open('dormitory/distribute/select?gender='+gender+'&id=' + id, __('设置寝室分配方式'));
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        select: function () {
            /*$(document).on('click', '.btn-back', function () {
                console.log(111);
            });*/
        },
        dorm: function () {

            $(document).on('change', '.building_number', function () {
                var garden_number = $('.garden_number').val();
                var building_number = $(this).val();
                var gender = $('#gender').val();

                $.ajax({
                    url: "dormitory/dorm/dormList",
                    type: 'post',
                    dataType: 'json',
                    data: {garden_number:garden_number, building_number: building_number, gender: gender},
                    success: function (ret) {
                        if (ret.code === 1) {

                            $('#floor_list').html(Template('floor_tpl', ret.data));
                            $('#dorm_list').html(Template('dorm_tpl', ret.data));

                        } else {
                            Backend.api.toastr.error(ret.msg);
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                });
            });

            // 全选
            $(document).on('click', '#check_all', function () {
                if(this.checked){
                    $(".floor").prop("checked",true);
                    $(".dorm").prop("checked",true);

                }else{
                    $(".floor").prop("checked",false);
                    $(".dorm").prop("checked",false);
                }

                countBed();
            });

            // 楼层
            $(document).on('click', ".floor", function () {

                var floor = $(this).val();

                if(this.checked){
                    if ($(".floor:not(:checked)").length === 0) {
                        $("#check_all").prop("checked",true);
                    }

                    $(".dorm_" + floor).prop("checked",true);

                } else {
                    $("#check_all").prop("checked",false);
                    $(".dorm_" + floor).prop("checked",false);
                }

                countBed();
            });

            // 寝室
            $(document).on('click', ".dorm", function () {

                var floor = $(this).data('floor');
                var $floor = $("#floor_" + floor);

                $floor.prop("indeterminate",true);

                if ($(".dorm_" + floor + ":checked").length === 0) {
                    $floor.prop("indeterminate",false);
                    $floor.prop("checked",false);
                }

                if ($(".dorm_" + floor + ":not(:checked)").length === 0) {
                    $floor.prop("indeterminate",false);
                    $floor.prop("checked",true);
                }

                countBed();
            });
            
            function countBed() {
                var selected_bed_count = 0;
                $('.dorm:checked').each(function () {
                    var bed_count = $(this).data('bed-count');
                    selected_bed_count += bed_count * 1;
                });

                $('#selected_bed_count').html(selected_bed_count);
            }

            Form.api.bindevent($("form#dorm"), function (data, ret) {
                $("#table").bootstrapTable('refresh');
            });

        },
        bed: function () {
            $(document).on('change', '.building_number', function () {
                var garden_number = $('.garden_number').val();
                var building_number = $(this).val();
                var gender = $('#gender').val();

                $.ajax({
                    url: "dormitory/dorm/dormList",
                    type: 'post',
                    dataType: 'json',
                    data: {garden_number:garden_number, building_number: building_number, gender: gender},
                    success: function (ret) {
                        if (ret.code === 1) {
                            $('#floor_list').html(Template('floor_tpl', ret.data));
                            $('#dorm_list').html(Template('dorm_tpl', ret.data));

                        } else {
                            Backend.api.toastr.error(ret.msg);
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                });
            });

            // 楼层
            $(document).on('change', '.floor', function () {

                var floor_array = [];
                $('input[name="floor[]"]:checked').each(function(){
                    floor_array.push($(this).val());//向数组中添加元素
                });

                var garden_number = $('.garden_number').val();
                var building_number = $('.building_number').val();
                var gender = $('#gender').val();

                $.ajax({
                    url: "dormitory/dorm/dormList",
                    type: 'post',
                    dataType: 'json',
                    data: {garden_number:garden_number, building_number: building_number, gender: gender, floor: floor_array},
                    success: function (ret) {
                        if (ret.code === 1) {
                            $('#dorm_list').html(Template('dorm_tpl', ret.data));
                        } else {
                            Backend.api.toastr.error(ret.msg);
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                });
            });

            // 寝室
            $(document).on('click', ".dorm", function () {

                var garden_number = $('.garden_number').val();
                var building_number = $('.building_number').val();
                //var dorm_number = $(this).val();

                var dorm_array = [];
                $('input[name="dorm[]"]:checked').each(function(){
                    dorm_array.push($(this).val());
                });

                $.ajax({
                    url: "dormitory/bed/bedList",
                    type: 'post',
                    dataType: 'json',
                    data: {garden_number:garden_number, building_number: building_number, dorm_number: dorm_array, status: 1},
                    success: function (ret) {
                        if (ret.code === 1) {
                            $('#bed_list').html(Template('bed_tpl', ret.data));
                        } else {
                            Backend.api.toastr.error(ret.msg);
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                });

            });

            $(document).on('click', ".bed", function () {
                countBed();
            });

            function countBed() {
                var selected_bed_count = 0;
                $('.bed:checked').each(function () {
                    selected_bed_count += 1;
                });

                $('#selected_bed_count').html(selected_bed_count);
            }


            Form.api.bindevent($("form#bed"), function (data, ret) {
                $("#table").bootstrapTable('refresh');
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});